package icasue.tools.compile.compilers.fmanager;

import icasue.tools.compile.compilers.jfobjs.AbstractJavaFileObjects;
import icasue.tools.compile.compilers.jfobjs.JavaFileObjectGen;

import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import java.io.IOException;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/12/3 下午3:51
 * @UpdateDate:
 * @Description:
 */
public class JavaFileManagerImpl extends ForwardingJavaFileManager<JavaFileManager> {
    /**
     * Creates a new instance of ForwardingJavaFileManager.
     *
     * @param fileManager delegate to this file manager
     */
    public JavaFileManagerImpl(JavaFileManager fileManager) {
        super(fileManager);
    }

    @Override
    public JavaFileObject getJavaFileForInput(Location location, String className, JavaFileObject.Kind kind) throws IOException {
        JavaFileObject javaFileObject = AbstractJavaFileObjects.objectsGeneric.get(className);
        if(javaFileObject == null){
            super.getJavaFileForInput(location, className, kind);
        }
        return javaFileObject;
    }

    @Override
    public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
        JavaFileObject javaFileObject = new JavaFileObjectGen(className, kind);
        AbstractJavaFileObjects.objectsGeneric.put(className, javaFileObject);
        return javaFileObject;
    }
}
